<?php

/**
 * 后台管理
 *
 * @package		Hooloo framework
 * @author 		Bill
 * @copyright 	Hooloo Co.,Ltd
 * @version		1.0
 * @release		2017.05.08
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Index extends Controller {
	public function __construct() {
		parent::__construct();
	}
	
	/**
     * 管理首页
     */
	public function index() {
		//代理商ID
		$aid = $_SESSION["agent"]["id"];
		
		//获取系统信息
		$week=array("日","一","二","三","四","五","六");
		$system_info = array(
            'server_domain' => $_SERVER['HTTP_HOST'] . ' [ ' . gethostbyname($_SERVER['HTTP_HOST']) . ' ]',
            'server_os' => PHP_OS,
            'web_server' => $_SERVER["SERVER_SOFTWARE"],
            'php_version' => PHP_VERSION,
            'mysql_version' => $this->db->getserver_info(),
            'upload_max_filesize' => ini_get('upload_max_filesize'),
            'max_execution_time' => ini_get('max_execution_time') . '秒',
            'safe_mode' => (boolean) ini_get('safe_mode') ?  "是" : "否",
            'zlib' => function_exists('gzclose') ?  "是" : "否",
            'curl' => function_exists("curl_getinfo") ?  "是" : "否",
            'timezone' => function_exists("date_default_timezone_get") ? date_default_timezone_get() : "否",
			'open_am_start' => $this->config_sys['open_am_start'],
			'open_am_end' => $this->config_sys['open_am_end'],
			'open_pm_start' => $this->config_sys['open_pm_start'],
			'open_pm_end' => $this->config_sys['open_pm_end'],
			'open_status' => get_holiday() == true ? "开市" : "休市",
        );
		
		//员工人数
		$sql = "select count(*) c from agent_staff where aid = $aid";
		$agent = $this->db->query($sql)->row_array();
		$other["staff_num"] = $agent["c"];
		
		//会员人数
		$sql = "select count(*) c from user where aid = $aid";
		$user = $this->db->query($sql)->row_array();
		$other["user_num"] = $user["c"];
		
		//委托单
		$sql = "select count(*) c from stock_trust where status = 0 and aid = $aid";
		$user = $this->db->query($sql)->row_array();
		$other["trust_num"] = $user["c"];
		
		//持仓单
		$sql = "select count(*) c from stock_bull where can_sell = 1 and aid = $aid";
		$user = $this->db->query($sql)->row_array();
		$other["bull_num"] = $user["c"];
		
		//充值入金-本月/周/日
		$start_month = date("Y-m-01 00:00:00");//本月第一天
		$sql = "select sum(money) as charge_m from log_charge where addtime >= '$start_month' and aid = $aid";
		$log_charge = $this->db->query($sql)->row_array();
		$other["charge_m"] = number_format($log_charge["charge_m"], 2);
		
		$start_week = date("Y-m-d H:i:s", mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y')));//本周第一天
		$sql = "select sum(money) as charge_w from log_charge where addtime >= '$start_week' and aid = $aid";
		$log_charge = $this->db->query($sql)->row_array();
		$other["charge_w"] = number_format($log_charge["charge_w"], 2);
		
		$start_day = date("Y-m-d");//今天
		$sql = "select sum(money) as charge_d from log_charge where addtime >= '$start_day' and aid = $aid";
		$log_charge = $this->db->query($sql)->row_array();
		$other["charge_d"] = number_format($log_charge["charge_d"], 2);
		
		//提现出金-本月/周/日
		$start_month = date("Y-m-01 00:00:00");//本月第一天
		$sql = "select sum(money) as atm_m from log_atm where uptime >= '$start_month' and ispay = 1 and aid = $aid";
		$log_atm = $this->db->query($sql)->row_array();
		$other["atm_m"] = number_format($log_atm["atm_m"], 2);
		
		$start_week = date("Y-m-d H:i:s", mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y')));//本周第一天
		$sql = "select sum(money) as atm_w from log_atm where uptime >= '$start_week' and ispay = 1 and aid = $aid";
		$log_atm = $this->db->query($sql)->row_array();
		$other["atm_w"] = number_format($log_atm["atm_w"], 2);
		
		$start_day = date("Y-m-d");//今天
		$sql = "select sum(money) as atm_d from log_atm where uptime >= '$start_day' and ispay = 1 and aid = $aid";
		$log_atm = $this->db->query($sql)->row_array();
		$other["atm_d"] = number_format($log_atm["atm_d"], 2);
		
		//融资、融券金额
		$sql = "select sum(round((bull_num - sell_num) * bull_price, 2)) as roz_money from stock_bull where bull_type = 1 and can_sell = 1 and aid = $aid";
		$stock_bull1 = $this->db->query($sql)->row_array();
		$other["roz_money"] = number_format($stock_bull1["roz_money"], 2);
		
		$sql = "select sum(round((bull_num - sell_num) * bull_price, 2)) as roq_money from stock_bull where bull_type = 2 and can_sell = 1 and aid = $aid";
		$stock_bull2 = $this->db->query($sql)->row_array();
		$other["roq_money"] = number_format($stock_bull2["roq_money"], 2);
		
		$other["all_money"] = number_format($stock_bull1["roz_money"] + $stock_bull2["roq_money"], 2);
		
        $this->assign('system_info', $system_info);
        $this->assign('other', $other);
		$this->display();
	}
	
	/**
     * 管理员登录
     * @param	string	$username	用户名
     * @param	string	$password 	密码
     * @param	array	$result 	返回结果
     */
	public function login() {
		
		//执行登录
		if (IS_POST) {
			//接收参数
			$username = isset($_POST["username"]) ? substr(sql_format($_POST["username"]), 0, 20) : "";
			$password = isset($_POST["password"]) ? $_POST["password"] : "";
			
			//校验参数
			if (! $username || ! $password) {
				ajax_return(0, "用户名或者密码不能为空");
			}
			
			//查找用户
			$sql = "select * from agent where username = '$username'";
			$agent = $this->db->query($sql)->row_array();
			if (! $agent) {
				ajax_return(0, "您输入的管理帐号不存在");
			}
			//判断登录次数
			if ($agent["is_locked"] ==1) {
				ajax_return(0, "您的帐号已被锁定，无法登录！");
			}
			//判断登录次数
			if ($agent["try_times"] > 10 && time() - $agent["last_try"] < 600) {
				ajax_return(0, "您登录次数太多，请10分钟后重试！");
			}
			
			//判断密码
			if (DEVELOPMENT_ENVIRONMENT == false && $agent["password"] != md5($password)) {
				//更新输错次数
				$sql = "update agent set try_times = try_times + 1, last_try = '" . time() . "', ip = '" . get_client_ip() . "' where username = '$username'";
				$this->db->query($sql);
				ajax_return(0, "您输入密码不正确，请重新输入");
			} else {
				//更新最后登录时间,输错次数为0
				$time = time();
				$sql = "update agent set try_times = 0, last_try = '$time', last_login = '$time', ip = '" . get_client_ip() . "' where username = '$username'";
				$this->db->query($sql);
				$agent["last_login"] = $time;
				$agent["mobile"] = empty($agent["mobile"]) ? "0371-53690183" : $agent["mobile"];
				//登录信息入session()
				$_SESSION["agent"] = $agent;
			}
			
			//返回登录信息
			ajax_return(1, "登录成功", $agent);
		} else {
			$this->display();
		}
	}
	
	/**
     * 退出登录
     */
	public function logout() {
		//删除session
		if (isset($_SESSION["agent"])) {
			unset($_SESSION["agent"]);
		}
		
		//返回结果集
		IS_AJAX && ajax_return(1, "退出成功");
		redirect("/index/login");
	}
	
	/**
	 * 清除系统缓存
	 * @return 	boolean	 $result 	结果集：true-成功，false-失败
	 */
	public function clear_system_cache() {
		clear_cache(TPL_COMPILE_PATH);
		clear_cache(CACHE_PATH);
		//返回结果集
		ajax_return(1, "缓存更新成功");
	}
}
